
import { Notification, Message, Loading } from 'element-ui'

import { Commit, Dispatch } from 'vuex';

import { get, defaultsDeep, cloneDeep } from 'lodash'

import { getDispatchAll, getDispatchDetail, getDispatchPV, confirmDispatch, createDispatch, singleDispatch, stopDispatch, qrcodeDispatch, updateDispatch, queryDispatchForm } from '@/api/dispatch'



export interface State {

}

const state: State = {
    noticeList: [],  //通知单
    dispatchList: [],//派工单详情
    processDetail: [], //工艺设计表
    dispatchForms: [], // 派工单列表
    totalForms: 0

};

const getters = {};

const mutations = {
    SET_NOTICE_LIST(state: any, payload: any) {
        state.noticeList = payload
    },
    SET_DISPATCH_LIST(state: any, payload: any) {
        state.dispatchList = payload
    },
    SET_PROCESS_DETAIL(state: any, payload: any) {
        state.processDetail = payload
    },
    SET_FORM_LIST(state: any, payload: any) {
        state.dispatchForms = payload
    },
    SET_TOTAL(state: any, payload: any) {
        state.totalForms = payload
    }
};

const actions = {



    //4-01查询通知单列表  /dispatch/notice
    async GET_ALL_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {
        let resp = await getDispatchAll(payload).catch((err: any) => {
            Notification.error(err)
        })
        if (resp) {
            let noticeList = get(resp, 'data', [])
            commit('SET_NOTICE_LIST', noticeList)
            //   Message.success('请求成功!')
            //  console.log(resp);
            return true
        }
    },

    //4-02查看派工详情   /dispatch/detail
    async GET_DETAIL_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {
        let resp = await getDispatchDetail(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            let dispatchList = get(resp, 'data', [])
            /* let dispatchList = 
                [{
                    id: 2, versionId: 4, producingId: 2, dispatchNumber: '202007010', number: 50, finishNumber: 0, dispatchTime: '2020-07-23 18:53', finishTime: null, state: '工序异常', remark: '0', processList: [{
                        id: 5, processSequenceId: 10, sequenceNumber: 1, processName: '材料', department: '采购', totalNumber: 50, single: true, state: '工序完工',
                        children: [{
                            id: null, equipment: '采购', debugHour: 0, quotaHour: 0, equipHourId: null,
                            children: [{ id: 5, workerId: 13, workerName: '袁晓丹', workerNumber: 50.0, realWorkHour: 0.0, numberQualified: 50.0, numberPending: 0, numberWasteMaterial: 0, numberWasteIndustrial: 0, numberRework: 0, single: true, singleConfirm: false, state: '质检完成' }]
                        }]
                    },
                    {
                        id: 6, processSequenceId: 11, sequenceNumber: 2, processName: '铸造', department: '加工', totalNumber: 50, single: false, state: '工序完工',
                        children: [{ id: 6, equipment: 'SK-1,SK-2', debugHour: 2, quotaHour: 2, equipHourId: 6, children: [{ id: 6, workerId: 14, workerName: '赵振春', workerNumber: 50.0, realWorkHour: 0.0, numberQualified: 49.0, numberPending: 1, numberWasteMaterial: 0, numberWasteIndustrial: 0, numberRework: 0, single: true, singleConfirm: true, state: '质检完成' }] }]
                    },
                    {
                        id: 7, processSequenceId: 12, sequenceNumber: 3, processName: '清整', department: '加工', totalNumber: 49, single: true, state: '工序异常',
                        children: [{
                            id: 7, equipment: '清整', debugHour: 0, quotaHour: 0, equipHourId: 7,
                            children: [{ id: null, workerId: 14, workerName: '赵振春', workerNumber: 50.0, realWorkHour: null, numberQualified: null, numberPending: null, numberWasteMaterial: null, numberWasteIndustrial: null, numberRework: null, single: null, singleConfirm: null, state: null }]
                        }]
                    },
                    { id: 8, processSequenceId: 13, sequenceNumber: 4, processName: '完工', department: '', totalNumber: 50, single: true, state: '未开始', children: [] }]
                }] */

            commit('SET_DISPATCH_LIST', dispatchList)
            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },


    //4-03-a获取工艺版本数据  /dispatch/getpv
    async GET_PV_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await getDispatchPV(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {



            let processDetail = get(resp, 'data', [])
            commit('SET_PROCESS_DETAIL', processDetail)
            //   Message.success('请求成功!')
            //  console.log(resp);
            return true
        }
    },

    //4-03-b派工 /dispatch/create
    async CREATE_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {
        let resp = await createDispatch(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')

            //  console.log('提交成功',resp);
            return true
        }
    },

    //4-04二维码  /dispatch/qrcode
    
        async QRCODE_DISPATCH(
            { state, commit, rootState, dispatch  }: any,
            payload: any
        ) {
    
            let resp = await qrcodeDispatch(payload).catch((err: any) => {
                Notification.error(err)
            })

            if (resp) {
               // let imgUrl = 'data:image/png;base64,' + btoa(new Uint8Array(resp.data).reduce((data, byte) => data + String.fromCharCode(byte),'))
                   
                //   Message.success('请求成功!')
               console.log("qrcode response success");
                return resp
            }
        },
        



    //4-05变更   /dispatch/updata
    async UPDATE_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await updateDispatch(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },

    //4-07终止派工单  /dispatch/stop
    async STOP_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await stopDispatch(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },

    //4-08单双机审核  /dispatch/single
    async SINGLE_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await singleDispatch(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },

    //4-09完工确认   /dispatch/confirm
    async CONFIRM_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await confirmDispatch(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },

    // 4-10 派工单查询
    async QUERY_DISPATCH(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await queryDispatchForm(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            console.log(resp);
            let formList = get(resp, 'data', [])
            let total = get(resp, 'total', 0)
            commit('SET_FORM_LIST', formList)
            commit('SET_TOTAL', total)
            return true
        }
    },

}


export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
};